/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.partaljevic.tapestry.gambling.pages;

import com.partaljevic.gambling.Customer;
import com.partaljevic.gambling.Identifiable;
import com.partaljevic.gambling.Privilege;
import com.partaljevic.tapestry.gambling.service.Customers;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 *
 * @author Danilo
 */
public class LogIn {

    @Property
    @Persist
    private String username;
    @Property
    private String password;
    @Inject
    private Customers customers;
    @InjectPage
    private Welcome welcome;
    @Persist("flash")
    private String message;
    private long ID;

    public long getID(){
        return ID;
    }

    Object onSubmitFromLoginForm()
    {
        for (Customer cust : customers.retrieve()) {
            if(cust.getUsername().equals(username) && cust.getPassword().equals(password)){
                if(isAdmin(cust))return AdminPage.class;
                ID = Identifiable.class.cast(cust).getID();
                welcome.setID(ID);
                return welcome;
            }
        }
        message = "Wrong username or password..";
        return null;

    }
    //prepraviti
    private boolean isAdmin(Customer customer){
        if(customer.getPrivilege()==Privilege.ADMINISTRATOR)return true;
        return false;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

        
}
